package com.oocl.project5.view;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

import com.oocl.project5.doublelinklist.Node;
import com.oocl.project5.doublelinklist.NodeFactory;
import com.oocl.project5.informationstore.Person;
import com.oocl.project5.operate.OperateNode;
import com.oocl.project5.parse.CommandParseImp;
import com.oocl.project5.printformat.PrintFormat;

public class View implements ViewController {
	private static Scanner scanner;

	public static void util() {
		// TODO Auto-generated method stub
		Node<Person> headPointer = NodeFactory.initPerson();
		CommandParseImp parseImp = new CommandParseImp();
		OperateNode operateNode = new OperateNode();
		System.out.println("Initialize successfully!");
		while(true){
			System.out.println("					A   Add ");
			System.out.println("					D   Delete ");
			System.out.println("					U   Update");
			System.out.println("					L   Look");
			System.out.println("					S   Sort");
			System.out.println("					Q   Qiut");
			System.out.println("					H   Help");
			System.out.println("Please input uppercase letter A、D、U、L、S or H to select the action you want");
			scanner = new Scanner(System.in);
			String command1 =scanner.next();
  			try {
				int s = parseImp.selectParse(command1);
				switch(s){
					case 0: {
						System.out.println("Quit");//break;
						System.exit(0);
						}
					case 1:{
						PrintFormat.printAddFormat();
						String command2 =scanner.next();
						try {
							operateNode.addNode(headPointer, parseImp.addParse(headPointer, command2));
							PrintFormat.printSucess();
						}finally{
							//PrintFormat.printPersonAll(headPointer);
							PrintFormat.printHomeInfor();
							if(System.in.read()!= 0)break;
						}
					}
					case 2:{
						PrintFormat.printDeleteFormat();
						String command2 =scanner.next();
						try {
							Person[] persons = parseImp.deleteParse(headPointer, command2);
							PrintFormat.printPerson(persons);
							PrintFormat.printConfirm();
							String command3 =scanner.next();
							if(command3.equals("Y"))
								operateNode.deleteNode(headPointer, persons);
								PrintFormat.printSucess();
						}finally{
							PrintFormat.printHomeInfor();
							if(System.in.read()!= 0)break;
						}
					}
					case 3:{
						PrintFormat.printFormat();
						String command2 =scanner.next();
						int n = parseImp.uParse(headPointer, command2);
						if(n==-1){
							System.out.println("The id not find !!!");
						}else{
						PrintFormat.printUpdataFormat();
						String command3 =scanner.next();
						try {
							Person[] persons = parseImp.updataParse(headPointer, n,command3);
							operateNode.updataNode(headPointer, persons,n);
							PrintFormat.printSucess();
							PrintFormat.printPersonAll(headPointer);
						}catch(Exception e){
							PrintFormat.printHomeInfor();
							if(System.in.read()!= 0)break;
						}
						}
						PrintFormat.printHomeInfor();
						if(System.in.read()!= 0)break;
					}
					case 4:
					{
						PrintFormat.printLookPrompt();
						String command2=scanner.next();
						String string = parseImp.lParse(headPointer, command2);
						if(string.equals("A")){
							PrintFormat.printHomeInfor();
							if(System.in.read()!= 0)break;
						}else if (string.equals("C")) {
							PrintFormat.printLookFormat();
							String command3=scanner.next();
							operateNode.lookNode(headPointer, parseImp.lookParse(headPointer, command3));
						}else{
							PrintFormat.printError();
						}
						PrintFormat.printHomeInfor();
						if(System.in.read()!= 0)break;
					}
					case 5:
					{
						System.out.println("Sort");
						PrintFormat.printSort();
						String command2=scanner.next();
						operateNode.sortNode(headPointer, parseImp.sortParse(headPointer, command2));
						PrintFormat.printSucess();
						PrintFormat.printHomeInfor();
						if(System.in.read()!= 0)break;
					}
					case 6:
					{
						PrintFormat.printHelps();
						PrintFormat.printHomeInfor();
						if(System.in.read()!= 0)break;
					}
					default:
					{
						PrintFormat.printError();
						PrintFormat.printHomeInfor();
						if(System.in.read()!= 0)break;
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				PrintFormat.printException();
			}
		}
		
	}
	
	public void commandProcess(InputStream in,OutputStream out){
		
	}
}
